/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.DAO.mysqlDAO;

import assignment_cp3120.DAO.TicketDAO;
import assignment_cp3120.dataObjects.Ticket;
import java.util.Vector;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
/**
 *
 * @author Richard Irons
 */
public class MysqlTicketDAO implements TicketDAO{
    public Vector<Ticket> getTicketList() {
        Vector<Ticket> tickets = new Vector();
        Connection c = MysqlDA0Factory.createConnection();
        String query = new String("SELECT * FROM TICKET");
        Ticket ticketTemp = new Ticket();

        try {
            Statement s = c.createStatement();
            ResultSet rs = s.executeQuery(query);            
            int i = 0;
            while(rs.next()) {
                ticketTemp = new Ticket();
                ticketTemp.setRefID(rs.getString("REF_ID"));
                ticketTemp.setFlightID(rs.getString("FLIGHT_ID"));
                ticketTemp.setTransactionID(rs.getString("TRANS_ID"));
                ticketTemp.setTicketPrice(Integer.parseInt(rs.getString("TICKET_PRICE")));
                tickets.add(ticketTemp);
            }
        }
        catch(SQLException e) {
            System.out.println(e.getMessage());
        }

        return tickets;
    }

    public void updateTickets(Ticket ticket) {
        Connection c = MysqlDA0Factory.createConnection();
        String price;
        price = Integer.toString(ticket.getTicketPrice());
        String update = new String("INSERT INTO TICKET (REF_ID, FLIGHT_ID, TRANS_ID, TICKET_PRICE)"
                        + "VALUES ("
                        + "\" " + "TK" + ticket.getRefID() + "\", "
                        + "\" " + ticket.getFlightID() + "\", "
                        + "\" " + ticket.getTransactionID() + "\", "
                        + "\" " + price + "\" "
                        + ")" );

        try {
            Statement statement = c.createStatement();
            statement.executeUpdate(update);
        } catch(SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}
